package com.ny.controller;

import com.ny.entity.Classrooms;
import com.ny.entity.Classrooms;
import com.ny.entity.Reservations;
import com.ny.entity.Users;
import com.ny.service.ClassroomService;
import com.ny.service.ReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@Controller
public class ApplyController {
    @Autowired
    private ClassroomService classroomService;
    @Autowired
    private ReservationService reservationService;
    @RequestMapping("/apply")
    public String apply(){

        return "apply";
    }
    @RequestMapping("/submitApply")
    public String submitApply(int classroomId, int userId, String startTime, String endTime, String reason, Model model) throws ParseException {

        classroomService.updateClassroomStatusById(classroomId, "申请中");
        System.out.println(classroomId);
        System.out.println(userId);
        System.out.println(startTime);
        System.out.println(endTime);
        System.out.println(reason);
        Reservations reservation = new Reservations();
        reservation.setReason(reason);
        reservation.setStatus("申请中");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
        reservation.setStartDatetime(sdf.parse(startTime));
        reservation.setEndDatetime(sdf.parse(endTime));

        Classrooms classrooms = new Classrooms();
        classrooms.setClassroomId(classroomId);
        reservation.setClassroom(classrooms);

        Users users = new Users();
        users.setUserId(userId);
        reservation.setUser(users);

        reservationService.addReservation(reservation);

        List<Classrooms> classroomsList = classroomService.findAllClassrooms();
        model.addAttribute("classroomList", classroomsList);
        return "classroom";
    }

}
